package com.huikeportal.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huikeportal.controller.common.Tools;
import com.huikeportal.dao.LoginMapper;
import com.huikeportal.dao.SelectSiteMapper;
import com.huikeportal.dao.UserSiteHistoryMapper;
import com.huikeportal.model.RegionModel;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.SiteSimpleInfo;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.UserSiteHistory;
import com.huikeportal.model.common.PageDataVo;
import com.huikeportal.model.common.SqlParameter;
import com.huikeportal.service.OverViewService;

@Service("overViewService")
public class OverViewServiceImpl implements OverViewService {

	@Autowired
	private SelectSiteMapper selectSiteMapper;

	@Autowired
	private UserSiteHistoryMapper userSiteHistoryMapper;

	@Autowired
	private LoginMapper loginMapper;

	// 分页查询店铺列表
	public PageDataVo getDpList(PageDataVo pageDataVo) throws Exception {
		int maxTotal = selectSiteMapper.queryDpCountByPage(pageDataVo);
		List<SiteAllInfo> dpList = selectSiteMapper
				.queryDpListByPage(pageDataVo);
		pageDataVo.setMaxRow(maxTotal);
		pageDataVo.setMaxPage((maxTotal + pageDataVo.getPageSize() - 1)
				/ pageDataVo.getPageSize());
		pageDataVo.setPageResult(dpList);
		return pageDataVo;
	}

	// 分页查询楼层列表
	public PageDataVo getLcList(PageDataVo pageDataVo) throws Exception {
		int maxTotal = selectSiteMapper.queryLcCountByPage(pageDataVo);
		List<SiteAllInfo> lcList = selectSiteMapper
				.queryLcListByPage(pageDataVo);
		pageDataVo.setMaxRow(maxTotal);
		pageDataVo.setMaxPage((maxTotal + pageDataVo.getPageSize() - 1)
				/ pageDataVo.getPageSize());
		pageDataVo.setPageResult(lcList);
		return pageDataVo;
	}

	// 分页查询通道列表
	public PageDataVo getTdList(PageDataVo pageDataVo) throws Exception {
		int maxTotal = selectSiteMapper.queryTdCountByPage(pageDataVo);
		List<SiteAllInfo> tdList = selectSiteMapper
				.queryTdListByPage(pageDataVo);
		pageDataVo.setMaxRow(maxTotal);
		pageDataVo.setMaxPage((maxTotal + pageDataVo.getPageSize() - 1)
				/ pageDataVo.getPageSize());
		pageDataVo.setPageResult(tdList);
		return pageDataVo;
	}

	// 分页查询区域列表
	public PageDataVo getQyList(PageDataVo pageDataVo) throws Exception {
		int maxTotal = selectSiteMapper.queryQyCountByPage(pageDataVo);
		List<SiteAllInfo> qyList = selectSiteMapper
				.queryQyListByPage(pageDataVo);
		pageDataVo.setMaxRow(maxTotal);
		pageDataVo.setMaxPage((maxTotal + pageDataVo.getPageSize() - 1)
				/ pageDataVo.getPageSize());
		pageDataVo.setPageResult(qyList);
		return pageDataVo;
	}

	// 获取当前用户该页面上一次选中的广场
	public String getLastSiteId(UserInfo userInfo, String menuAddress,
			String sysLang) throws Exception {
		// 查询菜单ID
		SqlParameter sql = new SqlParameter();
		sql.setString(menuAddress);
		sql.setSysLang(sysLang);
		Integer menuId = selectSiteMapper.queryMenuIdByAddress(sql);
		// 查询历史表编号
		UserSiteHistory userSiteHistory = new UserSiteHistory();
		userSiteHistory.setMenuId(menuId);
		userSiteHistory.setUserId(userInfo.getUserId());
		Integer id = selectSiteMapper.queryHistoryId(userSiteHistory);
		if (id == null) {
			return "";
		}
		UserSiteHistory site = userSiteHistoryMapper.selectByPrimaryKey(id);
		return site.getSiteId();
	}

	// 更新历史表
	public void updateLastSiteId(UserInfo userInfo, String menuAddress,
			String siteId, String sysLang) throws Exception {
		// 查询菜单ID
		SqlParameter sql = new SqlParameter();
		sql.setString(menuAddress);
		sql.setSysLang(sysLang);
		Integer menuId = selectSiteMapper.queryMenuIdByAddress(sql);
		// 查询历史表编号
		UserSiteHistory userSiteHistory = new UserSiteHistory();
		userSiteHistory.setMenuId(menuId);
		userSiteHistory.setUserId(userInfo.getUserId());
		Integer id = selectSiteMapper.queryHistoryId(userSiteHistory);
		// 更新历史表
		UserSiteHistory site = new UserSiteHistory();
		site.setId(id);
		site.setUpdateTime(new Date());
		site.setSiteId(siteId);
		userSiteHistoryMapper.updateByPrimaryKeySelective(site);
	}

	// 新增历史表
	public void insertLastSiteId(UserInfo userInfo, String menuAddress,
			String siteId, String sysLang) throws Exception {
		// 查询菜单ID
		SqlParameter sql = new SqlParameter();
		sql.setString(menuAddress);
		sql.setSysLang(sysLang);
		Integer menuId = selectSiteMapper.queryMenuIdByAddress(sql);
		// 新增历史表
		UserSiteHistory userSiteHistory = new UserSiteHistory();
		userSiteHistory.setCreateTime(new Date());
		userSiteHistory.setMenuId(menuId);
		userSiteHistory.setSiteId(siteId);
		userSiteHistory.setUserId(userInfo.getUserId());
		userSiteHistoryMapper.insertSelective(userSiteHistory);
	}

	// 查询下拉框结果集
	public List<SiteSimpleInfo> getComboResult(RegionModel regionModel,
			String type, UserInfo userInfo) throws Exception {
		List<SiteSimpleInfo> zeroList = new ArrayList<SiteSimpleInfo>();
		userInfo = loginMapper.getUserByName(userInfo);
		List<SiteAllInfo> gcList = getGcSiteList(userInfo);
		// 广场下区域
		if ("0".equals(type)) {
			if (!Tools.isHaveGcPer(gcList, regionModel.getGcSiteId())) {
				return zeroList;
			}
			return selectSiteMapper.getGcQyInQy(regionModel.getGcSiteId());
		}
		// 区域下区域
		else if ("1".equals(type)) {
			if (!Tools.isHaveGcPer(gcList, regionModel.getGcSiteId())) {
				return zeroList;
			}
			return selectSiteMapper.getQyQyInQy(regionModel.getGcSiteId());
		}
		// 楼层下区域
		else if ("2".equals(type)) {
			if (!Tools.isHaveGcPer(gcList, regionModel.getGcSiteId())) {
				return zeroList;
			}
			return selectSiteMapper.getLcQyInQy(regionModel.getGcSiteId());
		}
		// 店铺下区域
		else if ("3".equals(type)) {
			SqlParameter sql = new SqlParameter();
			sql.setString(regionModel.getGcSiteId());
			if (regionModel.getDpString() != null
					&& !"".equals(regionModel.getDpString())) {
				sql.setStringChar(regionModel.getDpString());
			}
			return selectSiteMapper.getDpQyInQy(sql);
		}
		// 广场下楼层
		else if ("4".equals(type)) {
			if (!Tools.isHaveGcPer(gcList, regionModel.getGcSiteId())) {
				return zeroList;
			}
			return selectSiteMapper.getGcLcInLc(regionModel.getGcSiteId());
		}
		// 区域下楼层
		else if ("5".equals(type)) {
			if (!Tools.isHaveGcPer(gcList, regionModel.getGcSiteId())) {
				return zeroList;
			}
			return selectSiteMapper.getQyLcInLc(regionModel.getGcSiteId());
		}
		// 楼层下楼层
		else if ("6".equals(type)) {
			if (!Tools.isHaveGcPer(gcList, regionModel.getGcSiteId())) {
				return zeroList;
			}
			return selectSiteMapper.getLcLcInLc(regionModel.getGcSiteId());
		}
		// 店铺下楼层
		else if ("7".equals(type)) {
			SqlParameter sql = new SqlParameter();
			sql.setString(regionModel.getGcSiteId());
			if (regionModel.getDpString() != null
					&& !"".equals(regionModel.getDpString())) {
				sql.setStringChar(regionModel.getDpString());
			}
			return selectSiteMapper.getDpLcInLc(sql);
		}
		// 广场下店铺
		else if ("8".equals(type)) {
			SqlParameter sql = new SqlParameter();
			sql.setString(regionModel.getGcSiteId());
			if (regionModel.getDpString() != null
					&& !"".equals(regionModel.getDpString())) {
				sql.setStringChar(regionModel.getDpString());
			}
			return selectSiteMapper.getGcDpInDp(sql);
		}
		// 区域下店铺
		else if ("9".equals(type)) {
			if (!Tools.isHaveGcPer(gcList, regionModel.getGcSiteId())) {
				return zeroList;
			}
			return selectSiteMapper.getQyDpInDp(regionModel.getGcSiteId());
		}
		// 楼层下店铺
		else if ("10".equals(type)) {
			if (!Tools.isHaveGcPer(gcList, regionModel.getGcSiteId())) {
				return zeroList;
			}
			return selectSiteMapper.getLcDpInDp(regionModel.getGcSiteId());
		}
		// 店铺下店铺
		else if ("11".equals(type)) {
			SqlParameter sql = new SqlParameter();
			sql.setString(regionModel.getGcSiteId());
			if (regionModel.getDpString() != null
					&& !"".equals(regionModel.getDpString())) {
				sql.setStringChar(regionModel.getDpString());
			}
			return selectSiteMapper.getDpDpInDp(sql);
		}
		// 广场下通道
		else if ("12".equals(type)) {
			if (!Tools.isHaveGcPer(gcList, regionModel.getGcSiteId())) {
				return zeroList;
			}
			return selectSiteMapper.getGcTdInTd(regionModel.getGcSiteId());
		}
		// 区域下通道
		else if ("13".equals(type)) {
			if (!Tools.isHaveGcPer(gcList, regionModel.getGcSiteId())) {
				return zeroList;
			}
			return selectSiteMapper.getQyTdInTd(regionModel.getGcSiteId());
		}
		// 楼层下通道
		else if ("14".equals(type)) {
			if (!Tools.isHaveGcPer(gcList, regionModel.getGcSiteId())) {
				return zeroList;
			}
			return selectSiteMapper.getLcTdInTd(regionModel.getGcSiteId());
		}
		// 店铺下通道
		else if ("15".equals(type)) {
			SqlParameter sql = new SqlParameter();
			sql.setString(regionModel.getGcSiteId());
			if (regionModel.getDpString() != null
					&& !"".equals(regionModel.getDpString())) {
				sql.setStringChar(regionModel.getDpString());
			}
			return selectSiteMapper.getDpTdInTd(sql);
		}
		return null;
	}

	/**
	 * 查询用户场所权限下所有广场
	 * 
	 * @param userInfo
	 * @return
	 * @throws Exception
	 */
	private List<SiteAllInfo> getGcSiteList(UserInfo userInfo) throws Exception {
		List<SiteAllInfo> gcList = new ArrayList<SiteAllInfo>();
		List<SiteAllInfo> siteList = loginMapper.queryUserSiteList(userInfo);
		for (int i = 0; i < siteList.size(); i++) {
			if (siteList.get(i).getType() == 300) {
				gcList.add(siteList.get(i));
			}
		}
		return gcList;
	}

}